package com.awanp3.nimbus.common.banner.runner;

import cn.hutool.core.thread.ThreadUtil;
import com.awanp3.nimbus.common.banner.enums.AnsiColor;
import com.awanp3.nimbus.common.banner.utils.AnsiAppender;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;

/**
 * banner printer
 *
 * @author 20014170
 */
@Slf4j
public class BannerApplicationRunner implements ApplicationRunner {

    @Value("${spring.application.name}")
    private String name;

    @Value("${spring.profiles.active}")
    private String env;

    @Value("${spring.application.version}")
    private String version;

    @Value("${server.port}")
    private String port;

    @Value("${spring.cloud.nacos.server-addr}")
    private String nacosServer;

    @Override
    public void run(ApplicationArguments args) {

        AnsiAppender appender = AnsiAppender.create()
            .append(AnsiColor.BRIGHT_GREEN, ":: " + name + " v" + version + " 服务启动成功(" + env + ") ::\n");
        // nacos
        appender.append(AnsiColor.BRIGHT_GREEN, ":: Nacos 控制台        ")
            .append(AnsiColor.BRIGHT_BLUE, nacosServer + "/nacos\n");

        appender.append(AnsiColor.BRIGHT_GREEN, ":: server 健康检测     ")
            .append(AnsiColor.BRIGHT_BLUE, "curl -X GET --location \"http://127.0.0.1:" + port  + "/actuator/health\"\n");

        if (name.contains("remote")) {
            appender.append(AnsiColor.BRIGHT_GREEN, ":: 远程接口服务，禁止从前端直接调用，请勿配置网关转发。::\n");
        }

        ThreadUtil.execute(
            () -> {
                ThreadUtil.sleep(1000L);
                System.out.println(appender);
            }
        );

    }
}
